package com.ysstech.finance.service.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ysstech.common.entity.Dicvalue;
import com.ysstech.common.service.DicvalueService;
import com.ysstech.finance.entity.Voucher;
import com.ysstech.finance.feign.FeignValuationService;
import com.ysstech.finance.feign.entity.StockDealDetailed;
import com.ysstech.finance.thread.VoucherGenerateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

@Slf4j
@Service
public class StockVoucherServiceImpl extends VoucherGenerateService<StockDealDetailed> {
    @Autowired
    private DicvalueService dicvalueService;
    @Autowired
    private FeignValuationService feignValuationService;

    /**
     * 凭证来源（股票业务凭证） 前台页面选择的凭证类型
     *
     * @return
     */
    @Override
    public String getVoucherSource() {
        return "STOCK_VOUCHER";
    }

    /**
     * 获取所有股票业务凭证下的凭证类型
     *
     * @return
     */
    @Override
    public List<String> getVoucherFtype() {
        List<Dicvalue> list = dicvalueService.queryDicValueList(getVoucherSource());
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        List<String> keys = new ArrayList<>();
        for (Dicvalue dicvalue : list) {
            if (null == dicvalue || StringUtils.isBlank(dicvalue.getValue())) {
                continue;
            }
            keys.add(dicvalue.getValue());
        }
        return keys;
    }

    @Override
    public List<StockDealDetailed> queryDatas(Voucher voucher) throws Exception {
        return feignValuationService.queryDatas(voucher.getAccountId(), voucher.getSelfdate());
    }

    @Override
    public Map<String, List<Object>> getVoucherBean(List<StockDealDetailed> datas) throws Exception {
        Map<String, List<Object>> map = new HashMap<>();
        for (StockDealDetailed securityDealDetailed : datas) {
            if (null == securityDealDetailed) {
                continue;
            }
            String key = this.getVoucherSource() + "_" + securityDealDetailed.getBusinessType()
                    + "_" + securityDealDetailed.getCurrency()
                    + "_" + securityDealDetailed.getMarket()
                    + "_" + securityDealDetailed.getInvestment()
                    + "_" + securityDealDetailed.getDealType()
                    + "_" + securityDealDetailed.getBusinessSign()
                    + "_" + securityDealDetailed.getStockType()
                    + "_" + securityDealDetailed.getListingAndCirculation();
            if (map.containsKey(key)) {
                List<Object> list = map.get(key);
                list.add(securityDealDetailed);
            } else {
                List<Object> list = new ArrayList<>();
                list.add(securityDealDetailed);
                map.put(key, list);
            }
        }
        return map;
    }
}
